package itsm.isperp.module.controller.incident;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.domain.IUser;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.incident.IcdMajorHandle;
import itsm.isperp.module.service.incident.IcdMajorHandleService;

import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2016-11-28 09:29
 */
@Controller
@RequestMapping("incident/incident/majorHandle")
public class IcdMajorHandleController extends BaseController<IcdMajorHandle> {

	@Autowired
	protected IcdMajorHandleService icdMajorHandleService;

	@Override
	public IcdMajorHandleService getService() {
		return this.icdMajorHandleService;
	}

	@Override
	public String getPageTitle() {
		return "重要事件";
	}

	@RequestMapping("save")
	@ResponseBody
	@Override
	public ResultMessage save(IcdMajorHandle model, HttpServletRequest req) {

		IUser user = ContextHolder.getLoginUser();
		model.setHandler(user.getUsername());
		model.setHandlerName(user.getFullname()); 
		this.getService().saveOrUpdate(model, req);
		return ResultMessage.newInstance(true, null, model.getId());
	}

	@RequestMapping("edit")
	@Override
	public ModelAndView edit(String id, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = this.getEdit(id);

		IcdMajorHandle model = (IcdMajorHandle) mv.getModel().get("model");

		String incidentId = req.getParameter("incidentId");
		if (StringUtils.isNotEmpty(incidentId)) {
			model.setIncidentId(incidentId);
		}
		
		if(model.getHandleTime()==null){
			
			model.setHandleTime(new Timestamp(new Date().getTime()));
		}

		return mv;

	}

	@RequestMapping("listByIncident")
	@ResponseBody
	public PageResponse<IcdMajorHandle> listByIncident(JqGridRequest request,
			HttpServletRequest req) {
		request.addFilterRule("incident_id", JqGridFilterRuleOp.eq,
				req.getParameter("incidentId"));
		request.setSort("handle_time",Direction.DESC);
		return this.getService().findAll(request).toPage();

	}
}
